package auctionhaus

import grails.orm.PagedResultList

class DefaultController {
    def listingService

    def index() {
        listingService.postCompletedListings()
        params.max = Math.min(params.max ? params.int('max') : 5, 100)
        params.offset = params.offset? params.offset : 0
        def instanceList = getCurrentListings(params.max, params.offset)
        [listingInstanceList: instanceList, listingInstanceTotal: getCurrentListingsCount()]
    }

    private def getCurrentListings (max, offset){
        def crit = Listing.createCriteria()
        def results = crit.list(max: max, offset: offset, sort: "dateCreated", order: "desc") {
            gt("endTime", new Date())
        }
        return results
    }

    private def getCurrentListingsCount() {
        def crit = Listing.createCriteria()
        def counter = crit.list(sort: "dateCreated") {gt("endTime", new Date())}
        return counter.toArray().length
    }
}
